package com.newdjk.newdoctor.utils;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;

import com.bumptech.glide.request.target.Target;
import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;

/*
 *  @项目名：  Doctor
 *  @包名：    com.newdjk.doctor.utils
 *  @文件名:   GlideUtils
 *  @创建者:   huhai
 *  @创建时间:  2019/10/14 11:48
 *  @描述：
 */
public class GlideUtils {


    public static void loadCommonmage(String path, ImageView imageView) {
        Glide.with(MyApplication.getContext())
                .load(path).
                apply(new RequestOptions()
                        .placeholder(R.drawable.new_nopic).
                                error(R.drawable.new_nopic))
                .into(imageView);
    }

    public static void loadMedicaineCommonmage(String path, ImageView imageView) {
        Glide.with(MyApplication.getContext())
                .load(path).
                apply(new RequestOptions()
                        .placeholder(R.drawable.yaopin).
                                error(R.drawable.yaopin))
                .into(imageView);
    }

    public static void loadCommonmage(String path, ImageView imageView, int placeholder) {
        Glide.with(MyApplication.getContext())
                .load(path).
                apply(new RequestOptions()
                        .placeholder(placeholder).
                                error(placeholder))
                .into(imageView);
    }

    public static void loadDoctorImage(String imagepath, ImageView view) {
        Glide.with(MyApplication.getContext())
                .load(imagepath).
                apply(new RequestOptions()
                        .placeholder(R.drawable.doctor_default_img).
                                error(R.drawable.doctor_default_img))
                .into(view);
    }

    public static void loadPatientImage(String imagepath, ImageView view) {
        Glide.with(MyApplication.getContext())
                .load(imagepath).apply(new RequestOptions()
                .placeholder(R.drawable.patient_default_img).
                        error(R.drawable.patient_default_img))
                .into(view);
    }

    public static void loadgif(String imagepath, ImageView view, LinearLayout adcontainer) {
        //设置图片大小
        RoundedCorners roundedCorners = new RoundedCorners(80);
        //通过RequestOptions扩展功能,
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
                .placeholder(new ColorDrawable(Color.GRAY))//设置占位图
                .error(R.drawable.new_nopic)//设置错误图片
                .fallback(new ColorDrawable(Color.RED))
                .override(80, 80);//
                 Glide.with(MyApplication.getContext()).asGif().load(imagepath).apply(options).error(R.drawable.new_nopic).listener(new RequestListener<GifDrawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {

                Log.d("Wain", "加载失败 errorMsg:" + (e != null ? e.getMessage() : "null"));

                return false;
            }

            @Override
            public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {

                Log.d("Wain", "成功  Drawable Name:" + resource.getClass().getCanonicalName());

                adcontainer.setVisibility(View.VISIBLE);
                return false;
            }
        }).into(view);


    }

    public static void loadCommonmage(String imgPath) {
    }
}
